package com.immoc.jdbc.sample;

import com.mysql.cj.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class StandardJDBC {
    public static void main(String[] args) {
        Connection conn = null;
        try{
        // 1. 加载注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 2. 创建数据库链接
        conn = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/immoc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai",
            "root",
            "455843572Lemonib"
        );
        // 3. 创建statement对象
        Statement statement = conn.createStatement();
        ResultSet rs = statement.executeQuery("SELECT * FROM employee");
        // 4. 遍历sql语句
        while(rs.next()){
            Integer eno = rs.getInt(1);
            String ename = rs.getString("ename");
            Float salary = rs.getFloat("salary");
            System.out.println(eno + "-" + ename +"-"+ salary);
        }}catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                // 5. 关闭连接，释放资源
                if (conn != null && conn.isClosed() == false) {
                    conn.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }
}
